package com.tsky.system.action;

import java.util.HashSet;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.tsky.casing.QueryBean;
import com.tsky.casing.action.BackAction;
import com.tsky.common.ActionUtil;
import com.tsky.common.BeanUtil;
import com.tsky.system.entity.Menu;
import com.tsky.system.entity.Role;
import com.tsky.system.json.JsonFormat;
import com.tsky.system.manager.RoleManager;
import com.tsky.system.query.RoleQuery;
@Component("roleAction")
@Scope("prototype")
public class RoleAction extends BackAction {

	@Autowired
	private RoleManager entityManager;

	/* bean properties */
	private String id;
	private String status;
	private String name;
	private String code;
	private String description;
	private String selectedMenus;

	private String ids;// 批量删除ID集合
	private String mids;// 菜单ID集合
	
	/**
	 * 角色菜单配置
	 * @author: cainiao
	 * @time: 2010-6-13 上午07:06:46
	 * @function:
	 */
	public void config(){
		Role entity = entityManager.getEntity(Role.class, id);
		BeanUtil.copyProperties(entity, this);
		
		String[] idset = mids.split(",");
		Set menus = new HashSet(entityManager.getAllEntity(Menu.class, idset));
		entity.setMenus(menus);
		
		boolean result = entityManager.updateEntity(entity);
		ActionUtil.ajaxWrite(result);
	}
	
	public void grid() {
		QueryBean grid = new RoleQuery();
		BeanUtil.copyProperties(grid, this);
		
		entityManager.query(grid);

		String jsonResult = JsonFormat.roleToJson(grid);
		ActionUtil.ajaxWrite(jsonResult);
	}

	public void add() {
		Role entity = new Role();
		BeanUtil.copyProperties(entity, this);
		entity.setId(BeanUtil.getID());
		entity.setCreateTime(BeanUtil.getTimeStamp());
		boolean result = entityManager.saveEntity(entity);
		ActionUtil.ajaxWrite(result);
	}

	public void delete() {
		String[] idset = ids.split(",");
		boolean result = entityManager.delEntity(Role.class, idset);
		ActionUtil.ajaxWrite(result);
	}

	public String query() {
		Role entity = entityManager.getEntity(Role.class, id);
		ActionUtil.setToEL("roleEnt", entity);
		
		return "query";
	}

	public void update() {
		Role entity = entityManager.getEntity(Role.class, id);
		BeanUtil.copyProperties(entity, this);
		boolean result = entityManager.updateEntity(entity);
		ActionUtil.ajaxWrite(result);
	}
	
	/* spring DI */
	public RoleManager getEntityManager() {
		return entityManager;
	}

	public void setEntityManager(RoleManager entityManager) {
		this.entityManager = entityManager;
	}

	/* simple getter and setter */
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getSelectedMenus() {
		return selectedMenus;
	}

	public void setSelectedMenus(String selectedMenus) {
		this.selectedMenus = selectedMenus;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getMids() {
		return mids;
	}

	public void setMids(String mids) {
		this.mids = mids;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}
	
}
